local gamelib = require "club.threecard.gamelib"
local slotlib = require "club.threecard.slotlib"
local element = slotlib.require "element"

local GameObjectExtension = CS.Blaze.GameObjectExtension
local Vector3 = CS.UnityEngine.Vector3
local TextMeshProUGUI = typeof(CS.TMPro.TextMeshProUGUI)

local M = gamelib.ui.element()

function M:ctor(symbols, pay_rules)
    self.symbols_config = symbols
    self.pay_rules = pay_rules
    self.res = "club.threecard.slotlib/ui/common.b:rule_combination"
    self.is_generated = false
    self.title = "赔付线奖励"
    self.nums = {}
    self.symbols_id = {}
    self.bonus = {}
    self.symbols_element = {}
    self.objs = {}
end

local function ana_config(self)
    for _, config in ipairs(self.pay_rules) do
        if config.pays then
            for _, pay_item in ipairs(config.pays) do
                if pay_item.type == 'Credits' then
                    local x = config.symbol_count
                    local y = config.symbol_id

                    if not self.symbols_id[y] and self.symbols_config[y] then
                        self.symbols_id[y] = true
                    end

                    if not self.nums[x] then
                        self.nums[x] = true
                    end

                    if not self.bonus[y] then
                        self.bonus[y] = {}
                    end

                    self.bonus[y][x] = pay_item.value
                end
            end
        end
    end

    local ids = {}
    for id, _ in pairs(self.symbols_id) do
        table.insert(ids, id)
    end
    table.sort(ids, function(id1, id2)
        return id1 < id2
    end)

    self.symbols_id = ids
end

local space_x = 230
local space_y = 180
local space_numlabel = 230
local function gen_symbols(self)
    local count = #self.symbols_id
    local start_pos_x = 0
    if count % 2 == 0 then
        start_pos_x = (count/2-0.5) * -space_x
    else
        start_pos_x = math.floor(count/2) * -space_x
    end


    for i, id in ipairs(self.symbols_id) do
        local config = self.symbols_config[id]
        if not config then
            print("xxxxxxxxxxxxxxx:"..id)
        end
        local e = element.new(config, 0, 200, 200)
        e:load()
        e:set_parent(self.content, false)
        e:set_position(Vector3(start_pos_x + (i-1) * space_x, 0, 0))
        table.insert(self.symbols_element, e)
    end
end

local function gen_bonus(self)
    gamelib.coroutine.step()
    for i, id in ipairs(self.symbols_id) do
        local e = self.symbols_element[i]
        if self.bonus[id] then
            local bonus = {}
            for num, _ in pairs(self.bonus[id]) do
                table.insert(bonus, num)
            end
            table.sort(bonus, function(a, b) return a>b end)

            for j, num in ipairs(bonus) do
                local obj = GameObjectExtension.GetFromPool(self.bonus_node)
                table.insert(self.objs, obj)
                obj.transform:SetParent(self.content, false)
                obj.transform.localPosition = Vector3(e.position.x, e.position.y - j * space_y, e.position.z)
                -- print("2bonus:id"..id.."num:"..num.."chip:")
                obj.transform:Find("value_txt").gameObject:GetComponent(TextMeshProUGUI).text = self.bonus[id][num]
            end
        end
    end 
end

local function gen_numlabel(self)
    gamelib.coroutine.step()

    local nums = {}
    for n, _ in pairs(self.nums) do
        table.insert(nums, n)
    end

    table.sort(nums, function(a, b) return a>b end)
    local first_e = self.symbols_element[1]
    for i, n in ipairs(nums) do
        local obj = GameObjectExtension.GetFromPool(self.numlabel_node)
        table.insert(self.objs, obj)
        obj.transform:SetParent(self.content, false)
        obj.transform.localPosition = Vector3(first_e.position.x - space_numlabel, first_e.position.y - i * space_y, first_e.position.z)
        obj.transform:Find("num_txt").gameObject:GetComponent(TextMeshProUGUI).text = string.format("%s种", n)
    end
end

local function gen_all(self)
    gen_symbols(self)
    gen_bonus(self)
    gen_numlabel(self)
    self.is_generated = true
end

function M:on_injected()
   self:generate() 
end

function M:generate()
    if self.is_generated then
        return
    end

    ana_config(self)
    self:start_coroutine(gen_all, self)
end

function M:on_release()
    self:super("on_release")
    self.is_generated = false
    for _, obj in pairs(self.objs) do
        GameObjectExtension.ReturnToPool(obj)
    end
    self.objs = nil
end

return M